package com.java.controller;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.domain.ActivityAdmin;
import com.java.domain.ActivityCoach;
import com.java.domain.ActivityCourse;
import com.java.domain.ActivityLaw;
import com.java.domain.ActivityRecord;
import com.java.domain.ActivityReview;
import com.java.domain.ActivityRoom;
import com.java.domain.ActivitySite;
import com.java.domain.ActivitySlot;
import com.java.domain.ActivitySport;
import com.java.domain.ActivityUser;
import com.java.service.AdminService;
import com.java.service.CoachService;
import com.java.service.CourseService;
import com.java.service.LawService;
import com.java.service.RecordService;
import com.java.service.ReviewService;
import com.java.service.RoomService;
import com.java.service.SiteService;
import com.java.service.SlotService;
import com.java.service.SportService;
import com.java.service.UserService;
import com.java.util.MyTools;
import com.java.util.resp.ApexChartRespone;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@RestController
@RequestMapping("/admin")
public class AdminDataController {
	@Autowired private AdminService adminService;
	@Autowired private CoachService coachService;
	@Autowired private CourseService courseService;
	@Autowired private LawService lawService;
	@Autowired private RecordService recordService;
	@Autowired private RoomService roomService;
	@Autowired private SiteService siteService;
	@Autowired private SlotService slotService;
	@Autowired private SportService sportService;
	@Autowired private UserService userService;
	@Autowired private ReviewService reviewService;
	
	@GetMapping(value = "/orderData")
	public ApexChartRespone orderData(String s, String e) {
		LocalDate start = MyTools.getLocalDate(s);
		if(null == start) {
			return new ApexChartRespone().error("Start Date Empty");
		}
		LocalDate end = MyTools.getLocalDate(e);
		if(null == end) {
			return new ApexChartRespone().error("End Date Empty");
		}
		long count = start.until(end, ChronoUnit.DAYS);
		if(count < 0) {
			return new ApexChartRespone().error("Date mismatch");
		}
		if(count > 7) {
			return new ApexChartRespone().error("Over 7 days");
		}
		List<String> clist = MyTools.getDatesBetween(start, end);
		List<String> dlist = new ArrayList<String>();
		for (String dateStr : clist) {
			dlist.add(recordService.getAmountOfDay(dateStr).toString());
		}
		return new ApexChartRespone().success(clist, dlist);
	}
	@GetMapping(value = "/sportData")
	public ApexChartRespone sportData(String s, String e) {
		LocalDate start = MyTools.getLocalDate(s);
		if(null == start) {
			return new ApexChartRespone().error("Start Date Empty");
		}
		LocalDate end = MyTools.getLocalDate(e);
		if(null == end) {
			return new ApexChartRespone().error("End Date Empty");
		}
		long count = start.until(end, ChronoUnit.DAYS);
		if(count < 0) {
			return new ApexChartRespone().error("Date mismatch");
		}
		if(count > 7) {
			return new ApexChartRespone().error("Over 7 days");
		}
		List<String> clist = MyTools.getDatesBetween(start, end);
		List<String> dlist = new ArrayList<String>();
		for (String dateStr : clist) {
			dlist.add(String.valueOf(sportService.getCountOfDay(dateStr)));
		}
		return new ApexChartRespone().success(clist, dlist);
	}
	
	@GetMapping(value = "/admins")
	public TableRespone admins(int page, int size) {
		return adminService.getList(page, size);
	}
	@PostMapping(value = "/saveAdmin")
	public BaseRespone saveAdmin(ActivityAdmin admin) {
		return adminService.saveAdmin(admin);
	}
	@GetMapping(value = "/delAdmin")
	public BaseRespone delAdmin(String adminId) {
		return adminService.delAdmin(adminId);
	}
	
	@GetMapping(value = "/coachs")
	public TableRespone coachs(int page, int size) {
		return coachService.getList(page, size, null);
	}
	@PostMapping(value = "/updateCoach")
	public BaseRespone updateCoach(ActivityCoach coach) {
		return coachService.saveCoach(coach);
	}
	@GetMapping(value = "/departCoach")
	public BaseRespone departCoach(String coachId) {
		return coachService.departCoach(coachId);
	}
	
	@GetMapping(value = "/courses")
	public TableRespone courses(int page, int size) {
		return courseService.getList(page, size);
	}
	@PostMapping(value = "/saveCourse")
	public BaseRespone saveCourse(ActivityCourse course) {
		return courseService.saveCourse(course);
	}
	@GetMapping(value = "/delCourse")
	public BaseRespone delCourse(String courseId) {
		return courseService.delCourse(courseId);
	}
	
	@GetMapping(value = "/laws")
	public TableRespone laws(int page, int size) {
		return lawService.getList(page, size);
	}
	@PostMapping(value = "/updateLaw")
	public BaseRespone saveLaw(ActivityLaw law) {
		return lawService.updateLaw(law.getLawId(), law.getLawRemark(), law.getLawContent());
	}
	
	@GetMapping(value = "/records")
	public TableRespone records(int page, int size) {
		return recordService.getList(null, page, size);
	}
	@PostMapping(value = "/updateRecord")
	public BaseRespone updateRecord(ActivityRecord record) {
		return recordService.updateRecord(record);
	}
	@GetMapping(value = "/confirmRecord")
	public BaseRespone confirmRecord(String recordId) {
		return recordService.confirmRecord(recordId, 1);
	}
	
	@GetMapping(value = "/rooms")
	public TableRespone rooms(int page, int size) {
		return roomService.getList(page, size, 0);
	}
	@PostMapping(value = "/saveRoom")
	public BaseRespone saveRoom(ActivityRoom room) {
		return roomService.saveRoom(room);
	}
	@GetMapping(value = "/delRoom")
	public BaseRespone delRoom(String roomId) {
		return roomService.delRoom(roomId);
	}
	
	@GetMapping(value = "/sites")
	public TableRespone sites(int page, int size) {
		return siteService.getList(page, size, 0);
	}
	@PostMapping(value = "/saveSite")
	public BaseRespone saveSite(ActivitySite site) {
		return siteService.saveSite(site);
	}
	@GetMapping(value = "/delSite")
	public BaseRespone delSite(String siteId) {
		return siteService.delSite(siteId);
	}
	
	@GetMapping(value = "/slots")
	public TableRespone slots(String sportId) {
		return slotService.getListBySport(sportId, null);
	}
	@PostMapping(value = "/updateSlot")
	public BaseRespone updateSlot(ActivitySlot slot) {
		return slotService.updateSlot(slot);
	}
	
	@GetMapping(value = "/sports")
	public TableRespone sports(Integer type, int page, int size) {
		return sportService.getListByType(type, null, null, page, size);
	}
	@PostMapping(value = "/saveSport")
	public BaseRespone saveSport(ActivitySport sport) {
		return sportService.saveSport(sport);
	}
	@GetMapping(value = "/delSport")
	public BaseRespone delSport(String sportId) {
		return sportService.delSport(sportId);
	}
	@GetMapping(value = "/publishSport")
	public BaseRespone publishSport(String sportId, int publish) {
		return sportService.sportPublish(sportId, publish);
	}
	
	@GetMapping(value = "/reviews")
	public TableRespone reviews(String sportId) {
		return reviewService.getReviewsBySport(sportId, 0, 0);
	}
	@PostMapping(value = "/saveReview")
	public BaseRespone saveReview(ActivityReview review) {
		return reviewService.replyReview(review.getReviewId(), review.getReviewReply());
	}
	@GetMapping(value = "/removeReview")
	public BaseRespone removeReview(String reviewId) {
		return reviewService.delReview(reviewId);
	}
	
	@GetMapping(value = "/users")
	public TableRespone users(int page, int size) {
		return userService.getList(page, size);
	}
	@PostMapping(value = "/updateUser")
	public BaseRespone updateUser(ActivityUser user) {
		return userService.updateUser(user);
	}
	@PostMapping(value = "/statusUser")
	public BaseRespone statusUser(String userId, int userStatus) {
		if(userStatus == 0) {
			return userService.unAvailableUser(userId);
		}
		return userService.availableUser(userId);
	}

}
